import box/{logger,condit} shell/termux

Check-Apk-Sign(){
  Has-File "$1" || { Err "找不到文件 $1";exit;}
  Info "正校验签名一致性..."
  __print_sha256(){ apksigner verify --print-certs "$1" | grep SHA-256 | cut -d: -f2;}
  local __base_file="$(pm path com.termux 2>&1 </dev/null | grep base.apk | sed 's|package:||')"
  local __base_sha256="$(__print_sha256 "$__base_file")"
  local __argfile_sha256="$(__print_sha256 "$1")"
  if ! In-Value __base_sha256 == __argfile_sha256;then
    Err "签名校验不通过，请使用官方签名的 Termux 应用"
    rm "$1"
    exit
  fi
}

Remove-Apk(){
  local APPS=($(yq 'del(.url) | keys[]' $TTB_DLYAML))

  local num=0
  for i in ${APPS[@]};do
      if [[ $1 == $i ]];then
          let num++
          break
      fi
  done

  if ! ((num));then
      Err "找不到应用 $1"
      exit
  else
      Has-Termux-Plugin $1 || { Warn "未安装应用 com.termux.$1";return;}
  fi
  Remove-Android-Package com.termux.$1
}

Install-Apk(){
    local property=~/.termux/termux.properties
    local release=$TTB_CONFIG/other/termux_release.txt
    local releases=($(yq '.url | keys[]' $TTB_DLYAML))

    Has-File $property || touch $property

    if ! [[ $(yq -Poy .allow-external-apps $property) == true ]];then
        Err "Termux 未启用 “允许外部应用在 Termux 内执行任何命令” 选项，请使用子命令 setting 设置:"
        Err "setting --env allow-external-apps=true"
        Err "随后再使用此功能"
        exit
    fi

    if ! Has-File $release || [ -z $(cat $release) ];then
        Get-List-Ui \
          --title="选择插件下载源" \
          --tip="按 ↑↓ 选择, 按 Ether 选中, 按 ESC 退出" \
          --print-choose --print-number --retract --beep \
          "${releases[@]}"
          local Choose=$[__return__-1]
          ((__return__)) || { Info "退出";exit;}
          echo ${releases[index]} > $release
    fi

    local app_release=$(cat $release)
    local URL=$(env=$app_release yq '.url.[strenv(env)]' $TTB_DLYAML)
    local APPS=($(yq 'del(.url) | keys[]' $TTB_DLYAML))

    if [ -z $1 ];then
        printf "%s\n" ${APPS[@]}
        exit
    fi

    if [[ $1 == --help ]];then
        echo "用法: install [应用]"
        echo "安装 Termux 所需插件应用"
        echo "空参数时将列出可安装应用"
        exit
    fi

    local num=0
    for i in ${APPS[@]};do
        if [[ $1 == $i ]];then
            let num++
            break
        fi
    done

    if ! ((num));then
        Err "找不到应用 $1"
        exit
    else
        if Has-Termux-Plugin $1;then
            Warn "已安装过应用 com.termux.$1"
            return
        fi
        local URL+=/$(env=$app_release app=$1 yq '.[strenv(app)].[strenv(env)]' $TTB_DLYAML)
        local DLNAME=termux-$1.apk
        Get-Ask-Ui "确认下载 Termux:${1^^} ?" yes
        case $__return__ in
            no|exit)Info "取消";exit
        esac
        Info "下载 $DLNAME ..."
        if wget --show-progress -qO $DLNAME $URL;then
            Info "termux-$1 下载完成"
        else
            Err "termux-$1 下载失败"
            exit
        fi
        Check-Apk-Sign "$DLNAME"
        Info "申请安装 termux-$1 应用..."
        # xdg-open ./$DLNAME
        am broadcast --user 0 -a android.intent.action.VIEW -n com.termux/com.termux.app.TermuxOpenReceiver -d $PWD/$DLNAME -W
        Info "回车以确认继续"
        read -s ENTER
        rm ./$DLNAME
    fi
    
}